<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">操作人：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="请输入"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">日期：</label>
                        <div class="layui-input-inline mr0">
                            <input name="dateRange" class="layui-input date-icon" type="text" placeholder="请选择日期范围"
                                   autocomplete="off"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">类型：</label>
                        <div class="layui-input-inline mr0">
                          <select name="type" lay-filter="type"  >
                            <option value=""  >--all--</option>
					        <option value="0"  >正常</option>
					        <option value="1" >业务</option>
					        <option value="2" >异常</option>
					      </select>
                        </div>
                    </div>                    
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">ip：</label>
                        <div class="layui-input-inline mr0">
                            <input name="ip" class="layui-input" type="text" placeholder="请输入"/>
                        </div>
                    </div>                    
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnExpLog" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableLog" lay-filter="tableLog"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarSysLogScript">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">查询</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate','admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var config = layui.config;
        var admin = layui.admin;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableLog',
            url: '#(ctx)/sys/log/listData',
            page: true,
            title: '登录日志',
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'name', sort: true, title: '操作人'},
                {field: 'action', sort: true, title: '请求地址'},
                {field: 'method', sort: true, title: '请求方式'},
                {field: 'ip', sort: true, title: 'IP'},
                {field: 'type', sort: true, title: '日志类型', templet: function (d) {
                	if('0'==d.type){
                		return'<span class="layui-badge layui-bg-green">系统</span>';
                	}else if('1'==d.type){
                		return'<span class="layui-badge layui-bg-blue">业务</span>';
                	}else if('2'==d.type){
                		return'<span class="layui-badge">错误</span>';
                	}else{
                		return'<span class="layui-badge  layui-bg-gray">其它</span>';
                	}
                    return util.toDateString(d.requestTime);
                }},
                {
                    field: 'requestTime', templet: function (d) {
                        return util.toDateString(d.requestTime);
                    }, title: '请求时间'
                },
                {
                    field: 'responseTime', templet: function (d) {
                        return util.toDateString(d.responseTime);
                    }, title: '响应时间'
                },
                {align: 'center', toolbar: '#tableBarSysLogScript', title: '操作', minWidth: 150}
            ]]
        });

        // 时间范围
        laydate.render({
            elem: 'input[name="dateRange"]',
            type: 'date',
            range: true
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            if (data.field.dateRange) {
                var searchDate = data.field.dateRange.split(' - ');
                data.field.startDate = searchDate[0];
                data.field.endDate = searchDate[1];
            }
            data.field.dateRange = undefined;
            insTb.reload({where: data.field}, 'data');
        });
        form.render(); 
        // 导出excel
        $('#btnExpLog').click(function () {
            var checkRows = table.checkStatus('tableLog');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });
        
        // 工具条点击事件
        table.on('tool(tableLog)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'view') { // 修改
            } else if (layEvent === 'del') { // 删除
                doDel(obj);
            }
        });
        
        // 删除
        function doDel(obj) {
            layer.confirm('确定要删除“' + obj.data.name + '”数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('#(ctx)/sys/log/delete', {
                    id: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.success) {
                        layer.msg(res.message, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }
    });
</script>